//
//  LiuComposeStackView.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/4/5.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

enum LiuComposeStackViewButtonType:Int {
    
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  #
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
    
}

class LiuComposeStackView: UIStackView {
    
    //标记emoticon按钮
    fileprivate var emoticonButton:UIButton?
    
    //回调的闭包
    var buttonActionClosure: ((LiuComposeStackViewButtonType)->())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        
        //添加按钮
        addChildButton(imageName: "compose_camerabutton_background", type: .picture)
        addChildButton(imageName: "compose_mentionbutton_background", type: .mention)
        addChildButton(imageName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addChildButton(imageName: "compose_add_background", type: .add)
        
        //水平分布
        axis = .horizontal
        
        //均分
        distribution = .fillEqually
    }
    @discardableResult
    fileprivate func addChildButton(imageName:String, type:LiuComposeStackViewButtonType)->UIButton {
        
        let button = UIButton()
        button.tag = type.rawValue
        
        //添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setImage(UIImage(named:"\(imageName)_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
        return button
        
    }
    
    //点击事件
    @objc fileprivate func buttonAction(btn:UIButton) {
        //通过原始值获取枚举值
        let buttontype = LiuComposeStackViewButtonType(rawValue: btn.tag)
        
        buttonActionClosure?(buttontype!)
        
    }
    
    //改变button的图片
    func changeImage(isChange:Bool) {
        if isChange {
            emoticonButton!.setImage(UIImage(named:"compose_keyboardbutton_background"), for: .normal)
            emoticonButton!.setImage(UIImage(named:"compose_keyboardbutton_background_highlighted"), for: .highlighted)
        } else {
            emoticonButton!.setImage(UIImage(named:"compose_emoticonbutton_background"), for: .normal)
            emoticonButton!.setImage(UIImage(named:"compose_emoticonbutton_background_highlighted"), for: .highlighted)
        }
        
    }

}
